package com.panfeng.xcloud.boss.provider.member.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.panfeng.xcloud.boss.provider.member.aop.CacheHandler;
import com.panfeng.xcloud.boss.provider.member.dto.request.AddNoticeRelationReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.request.NoticeMsgReqDTO;
import com.panfeng.xcloud.boss.provider.member.dto.response.NoticeMsgPageRespDTO;
import com.panfeng.xcloud.boss.provider.member.service.INoticeService;
import com.panfeng.xcloud.boss.provider.member.web.vo.request.AddNoticeRelationReqVO;
import com.panfeng.xcloud.boss.provider.member.web.vo.request.NoticeMsgReqVO;
import com.panfeng.xcloud.boss.provider.member.web.vo.response.NoticeMsgPageRespVO;
import com.panfeng.xcloud.common.core.constants.CacheHandlerConstants;
import com.panfeng.xcloud.common.core.page.PageResponse;
import com.panfeng.xcloud.common.core.utils.ConvertUtils;
import com.panfeng.xcloud.common.core.web.vo.PageResponseVO;
import com.panfeng.xcloud.common.core.web.vo.ResponseVO;
import com.panfeng.xcloud.common.security.annotation.MethodValidate;
import com.panfeng.xcloud.common.security.annotation.NotNeedAccess;
import com.panfeng.xcloud.common.security.web.support.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * 消息公告模块
 *
 * @author xiaobo
 * @version 1.0
 * @since 2019-08-08
 */
@ApiIgnore
@RestController
@RequestMapping("user/notice")
@Api(tags = {"消息公告模块"})
@Slf4j
public class UserNoticeController extends BaseController {

    @Autowired
    private INoticeService iNoticeService;

    @CacheHandler(keyPrefix = CacheHandlerConstants.XDCLOUD_QUERYNOTICEMSG_PREFIX ,expire = 60 * 2)
    @MethodValidate
    @NotNeedAccess
    @PostMapping(value = "/queryNoticeMsg")
    @ApiOperation(value = "公告消息查询(肖博)", httpMethod = "POST")
    public ResponseVO<PageResponseVO<NoticeMsgPageRespVO>> queryNoticeMsg(@Validated @RequestBody NoticeMsgReqVO noticeMsgReqVO, BindingResult bindingResult) {
        log.info(">>>> 当前queryNoticeMsg请求参数：{} <<<",(null != noticeMsgReqVO ? JSONObject.toJSONString(noticeMsgReqVO):""));
        NoticeMsgReqDTO noticeMsgReqDTO = ConvertUtils.convert(noticeMsgReqVO, NoticeMsgReqDTO.class);
        PageResponse<NoticeMsgPageRespDTO> pageDTO = iNoticeService.queryNoticeMsg(noticeMsgReqDTO);
        log.info(">>>> 当前queryNoticeMsg响应报文pageDTO：{} <<<",(null != pageDTO ? JSONObject.toJSONString(pageDTO) : ""));
        PageResponseVO<NoticeMsgPageRespVO> pageVO = ConvertUtils.convertPage(pageDTO, NoticeMsgPageRespVO.class);
        log.info(">>>> 当前queryNoticeMsg响应报文pageVO：{} <<<",(null != pageVO ? JSONObject.toJSONString(pageVO) : ""));
        return ResponseVO.with(pageVO);
    }

    /**
     * 添加用户消息公告关系
     *
     * @param addNoticeRelationReqVO
     * @param bindingResult
     * @return
     */
    @MethodValidate
    @PostMapping(value = "/addUserNoticeRelation", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "添加用户消息公告关系(肖博)", httpMethod = "POST", notes = "添加用户消息公告关系(肖博)")
    public ResponseVO addUserNoticeRelation(@RequestBody @Valid AddNoticeRelationReqVO addNoticeRelationReqVO,BindingResult bindingResult) {
        logger.info(">>>> 当前addUserNoticeRelation请求参数：{} <<<", (null != addNoticeRelationReqVO ? JSONObject.toJSONString(addNoticeRelationReqVO) : ""));
        AddNoticeRelationReqDTO addNoticeRelationReqDTO = ConvertUtils.convert(addNoticeRelationReqVO, AddNoticeRelationReqDTO.class);
        boolean rs = iNoticeService.addUserNoticeRelation(addNoticeRelationReqDTO);
        return rs ? ResponseVO.success() : ResponseVO.error();
    }

}
